package cn.mujiankeji.mbrowser.界面.标签页.快照

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import cn.mujiankeji.mbrowser.app.应用组件.应用图片
import cn.mujiankeji.mbrowser.app.应用路由
import cn.mujiankeji.mbrowser.工具.操作.地址操作
import cn.mujiankeji.mbrowser.数据.枚举类.页面状态枚举
import cn.mujiankeji.mbrowser.数据.模型类.基本项目
import cn.mujiankeji.mbrowser.数据.快照来源.快照源_章节列表
import cn.mujiankeji.mbrowser.数据.快照来源.快照来源接口
import cn.nr19.jian.E2Factory
import cn.nr19.jian.`object`.JianLei
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class 快照状态_章节列表(val 来源 : 快照源_章节列表) : 快照状态() {
    var 小说名称 by mutableStateOf("")
    var 作者 by mutableStateOf("")
    var 分组 by mutableStateOf("")
    var 简介 by mutableStateOf("")
    var 封面 by mutableStateOf("")
    var 章节列表 = mutableStateListOf<基本项目>()

    var 上一页地址 by mutableStateOf<String?>(null)
    var 下一页地址 by mutableStateOf<String?>(null)

    override fun 来源(): 快照来源接口 = 来源

    init {
        super.地址.value = 来源.地址
    }

    override suspend fun CoroutineScope.加载(code: String) {
        super.加载进度.value = 80

        val jian = JianLei()

        // 名称
        launch {
            小说名称 = tryText(code,来源.标题, jian)
            来源.封面?.let { 封面 = tryText(code,it,jian) }
            来源.作者?.let { 作者 = tryText(code,it, jian) }
            来源.分组?.let { 分组 = tryText(code,it, jian) }
            来源.简介?.let { 简介 = tryText(code,it, jian) }
            来源.章节列表上一页?.let { 上一页地址 = tryText(code,it,jian) }
            来源.章节列表下一页?.let { 下一页地址 = tryText(code,it,jian) }
            super.标题.value = 小说名称.ifEmpty { super.地址.value }
            super.加载进度.value = 100
            super.状态.value = 页面状态枚举.完成
        }


        // list
        launch {
            tryList(code,来源.章节列表,jian)?.forEach { scope->
                基本项目(
                    标题 = E2Factory.text(scope,来源.章节项目标题,jian),
                    地址 = E2Factory.text(scope,来源.章节项目地址,jian),
                ).also {章节列表.add(it)}
            }
        }


    }


    fun 开始阅读(){

    }

    fun 跳转(url : String){
        应用路由.加载(地址操作.取绝对地址(url,来源.地址))
    }
}

@Composable
fun 快照状态_章节列表(state : 快照状态_章节列表){
    快照页面布局(state,true){
        LazyColumn(Modifier.fillMaxSize()){

            // 标题板块
            item {
                Row(Modifier.fillMaxWidth()
                    .padding(20.dp)
                    .height(110.dp))
                {
                    应用图片(state.封面?:"",
                        modifier = Modifier.size(80.dp,110.dp))

                    Spacer(Modifier.width(12.dp))

                    Column(Modifier.weight(1f).fillMaxHeight())
                    {

                        Text(state.小说名称,
                            modifier = Modifier.fillMaxWidth(),
                            style = MaterialTheme.typography.titleMedium,
                            color = MaterialTheme.colorScheme.onSurface)

                        Spacer(Modifier.weight(1f))


                        Text("源地址: ${state.来源.地址}",
                            style = MaterialTheme.typography.labelMedium,
                            maxLines = 1)

                        Spacer(Modifier.height(5.dp))

                        Text("作者: ${state.作者}",
                            style = MaterialTheme.typography.bodyMedium,
                            maxLines = 1)

                        Text("分组: ${state.分组}",
                            style = MaterialTheme.typography.bodyMedium,
                            maxLines = 1)

                    }

                }
            }

            item {
                Button({state.开始阅读()}, modifier = Modifier.padding(20.dp).fillMaxWidth()) {
                    Text("开始阅读")
                }
            }

            // 简介
            item {
                Column(Modifier.padding(20.dp).fillMaxWidth()) {
                    Text("简介", style = MaterialTheme.typography.titleMedium)
                    Spacer(Modifier.height(10.dp))
                    Text(state.简介, style = MaterialTheme.typography.bodyMedium)
                }
            }


            // 列表
            item {
                Box(Modifier.padding(15.dp)){
                    Text("章节列表", style = MaterialTheme.typography.titleMedium)
                }
            }
            items(state.章节列表){
                Row(Modifier.fillMaxWidth()
                    .clickable {
                        state.跳转(it.地址)
                    }
                    .height(40.dp)
                    .padding(horizontal = 15.dp),
                    verticalAlignment = Alignment.CenterVertically)
                {
                    Text(it.标题, style = MaterialTheme.typography.bodyMedium,
                        maxLines = 1)
                }
            }


        }
    }

}

